import { isEmpty } from 'lodash-es'

import { queryUserInfoApi } from '@/api/sys/organization'
import modelExtend from '@/utils/dvaUtil/modelExtend'

export const namespace = 'personalCenter'

const initialState = {
  userInfo: {},
}

export default modelExtend({
  namespace,
  state: initialState,
  matchedActions: toAction => {
    toAction('getUserInfo')
  },
  effects: {
    *getUserInfo(_, { call, update, select }) {
      let { userInfo, loginInfo } = yield select(store => store[namespace])
      if (!isEmpty(userInfo)) return
      const { data } = yield call(queryUserInfoApi)
      userInfo = data || {}
      if (loginInfo?.superAdmin) {
        userInfo.organizationName = '医贝'
        userInfo.fullName = '超级管理员'
      }
      yield update({ userInfo })
    },
  },
})
